package com.ruoyi.web.controller.store.pan;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.VendorSettlement;
import com.ruoyi.common.core.domain.entity.VendorSettlementDetail;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.IVendorSettlementDetailService;
import com.ruoyi.system.service.IVendorSettlementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * detailController
 *
 * @author admin
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/settlement/detail")
@Api(tags = "detail相关接口")
public class VendorSettlementDetailController extends BaseController
{
    @Autowired
    private IVendorSettlementDetailService vendorSettlementDetailService;

    @Resource
    IVendorSettlementService vendorSettlementService;


    /**
     * 查询detail列表
     */
    @GetMapping("/list")
    @ApiOperation("查询detail列表")
    public TableDataInfo list(VendorSettlementDetail vendorSettlementDetail)
    {
            startPage();
            List<VendorSettlementDetail> list = vendorSettlementDetailService.selectVendorSettlementDetailList(vendorSettlementDetail);
            return getDataTable(list);
    }

    /**
     * 导出detail列表
     */
//    @PreAuthorize("@ss.hasPermi('settlement:detail:export')")
    @GetMapping("/export")
    @ApiOperation("导出detail列表")
    public AjaxResult export(VendorSettlementDetail vendorSettlementDetail)
    {
            List<VendorSettlementDetail> list = vendorSettlementDetailService.selectVendorSettlementDetailList(vendorSettlementDetail);
            ExcelUtil<VendorSettlementDetail> util = new ExcelUtil<VendorSettlementDetail>(VendorSettlementDetail.class);
            return util.exportExcel(list, "detail");
    }

    /**
     * 获取detail详细信息
     */
//    @PreAuthorize("@ss.hasPermi('settlement:detail:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取detail详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
            return AjaxResult.success(vendorSettlementDetailService.selectVendorSettlementDetailById(id));
    }

    /**
     * 新增detail
     */
//    @PreAuthorize("@ss.hasPermi('settlement:detail:add')")
    @PostMapping
    @ApiOperation("新增detail")
    @RepeatSubmit
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody VendorSettlementDetail vendorSettlementDetail)
    {
            return toAjax(vendorSettlementDetailService.insertVendorSettlementDetail(vendorSettlementDetail));
    }

    /**
     * 修改detail
     */
//    @PreAuthorize("@ss.hasPermi('settlement:detail:edit')")
    @PutMapping
    @ApiOperation("修改detail")
    @RepeatSubmit
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult edit(@RequestBody VendorSettlementDetail query)
    {
        VendorSettlementDetail  vendorSettlementDetail = vendorSettlementDetailService.selectVendorSettlementDetailById(query.getId());
        vendorSettlementDetail.setSettlementPrice(query.getSettlementPrice());
        vendorSettlementDetail.setSettlementMoney(query.getSettlementMoney());
        vendorSettlementDetailService.updateVendorSettlementDetail(vendorSettlementDetail);//先修改 在查询所有的

        VendorSettlement search = new VendorSettlement();
        search.setBusinessNum(vendorSettlementDetail.getBusinessNum());
        List<VendorSettlement> vendorSettlement = vendorSettlementService.selectVendorSettlementList(search);

        //查所有的明细
        VendorSettlementDetail cx = new VendorSettlementDetail();
        cx.setBusinessNum(search.getBusinessNum());
        List<VendorSettlementDetail> details =vendorSettlementDetailService.selectVendorSettlementDetailList(cx);

        //修改主表金额
        vendorSettlement.get(0).setMoney(details.stream().map(VendorSettlementDetail::getSettlementMoney).reduce(BigDecimal.ZERO,BigDecimal::add));
        vendorSettlementService.updateVendorSettlement(vendorSettlement.get(0));

        return AjaxResult.success();
    }

    /**
     * 删除detail
     */
//    @PreAuthorize("@ss.hasPermi('settlement:detail:remove')")
	@DeleteMapping("/{ids}")
    @ApiOperation("删除detail")
    @RepeatSubmit
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Long[] ids)
    {
            return toAjax(vendorSettlementDetailService.deleteVendorSettlementDetailByIds(ids));
    }
}
